package com.foreknow.dao.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;
import com.foreknow.model.Guestbook;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class AdminDaoImpl extends BaseDAO implements AdminDao {



    @Override
    public boolean register(Admin admin) throws SQLException {
        String sql="insert into admin (userName,password) values(?,?)";
        int update = jdbcTemplate.update(sql, admin.getAdminName(), admin.getPassword());
        return update==1;

    }

    @Override
    public Admin isLogin(String username, String password) throws  SQLException {
        String sql = "select * from admin where username=?";
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,username);
        if(list.size()>0) {
            Admin admin = (Admin) list.get(0);
            if(admin.getAdminName().equals(username)&&admin.getPassword().equals(password)){
                return admin;
            }
        }
        return null;
    }

    @Override
    public List<Object> getAll() throws  SQLException {
        String sql="select * from admin";
        EntityMapping map = mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        List<Object> query = jdbcTemplate.query(sql, map);

        return query;
    }

}
